var reqBaseRoute = "/9.1";
var successCode = 1; //正确状态码
var errorCode = -100; //错误状态码
var reqRoute = {
	"repositoryType": reqBaseRoute + "/enums/repositoryType", //资源库类型
	"databaseType": reqBaseRoute + "/enums/databaseType", //数据库类型列表
	"databaseAccessType": reqBaseRoute + "/enums/databaseAccessType", //数据库访问类型枚举
	"repositoryList": reqBaseRoute + "/repository/list", //查询资源库列表（分页）
	"repositoryTestConn": reqBaseRoute + "/repository/testConnection", //测试资源库链接
	"repositoryAdd": reqBaseRoute + "/repository/add", //资源库新增
	"repositoryGetById": reqBaseRoute + "/repository/getById", //通过id查询资源库
	"repositoryEdit": reqBaseRoute + "/repository/update", //资源库更新
	"repositoryGetById": reqBaseRoute + "/repository/getById", //根据id查询资源库
	"repositoryEdit": reqBaseRoute + "/repository/update", //资源库编辑
	"repositoryDel": reqBaseRoute + "/repository/delete", //根据id删除资源库
	"getDatabasesByRepId": reqBaseRoute + "/repository/getDatabasesByRepId", //根据资源库id获取资源库下数据库连接
	"showPwd": reqBaseRoute + "/repository/showPwd", //显示密码
	"sharedDb": reqBaseRoute + "/repository/sharedDb", //共享数据库
	"closeSharedDb": reqBaseRoute + "/repository/closeSharedDb", //取消共享数据库
	"findTree": reqBaseRoute + "/dicategory/findTree", //查询资源库目录树
	"dirCreate": reqBaseRoute + "/dicategory/createDir", //创建目录
	"dirUpdate": reqBaseRoute + "/dicategory/modifyDir", //重命名目录
	"dirDelete": reqBaseRoute + "/dicategory/deleteDir", //删除目录
	"findScriptById": reqBaseRoute + "/script/findScriptById", //查询目录下的脚本
	"scriptDel": reqBaseRoute + "/script/delete", //删除脚本
	"scriptUpload": reqBaseRoute + "/script/upload", //上传脚本
	"scriptRename": reqBaseRoute + "/script/rename", //重命名脚本
	"gparamType": reqBaseRoute + "/enums/paramType", //参数类型
	"gparamPage": reqBaseRoute + "/param/page", //参数列表（分页）
	"gparamAdd": reqBaseRoute + "/param/add", //参数新增
	"gparamById": reqBaseRoute + "/param/getById", //根据ID查询参数
	"gparamEdit": reqBaseRoute + "/param/edit", //参数编辑
	"gparamDel": reqBaseRoute + "/param/delete", //参数删除
	"taskPage": reqBaseRoute + "/task/page", //任务列表（分页）
	"taskAdd": reqBaseRoute + "/task/add", //任务新增
	"taskById": reqBaseRoute + "/task/findById", //根据ID查询任务信息
	"taskEdit": reqBaseRoute + "/task/edit", //任务编辑
	"taskDel": reqBaseRoute + "/task/delete", //任务删除
	"taskStart": reqBaseRoute + "/task/start", //任务启动
	"taskStop": reqBaseRoute + "/task/stop", //任务停止
	"parseCron": reqBaseRoute + "/task/parseCron", //解析cron
	"taskRecordPage": reqBaseRoute + "/task-record/page", //任务日志列表（分页）
	"taskRecordDown": reqBaseRoute + "/task-record/download" //任务日志文件下载
};
var pageRoute = {
	"repoList": "/views/repository/repoList.html", //资源库列表
	"repoAdd": "/views/repository/repoAdd.html", //资源库新增
	"repoEdit": "/views/repository/repoEdit.html", //资源库编辑
	"repoDbList": "/views/repository/dbList.html", //资源数据源列表页
	"dataInteList": "/views/data_integration/list.html", //数据整合列表
	"paramList": "/views/global_param/gpList.html", //参数管理
	"help": "/views/system/help.html", //帮助页面
	"taskList": "/views/task/taskList.html", //任务管理
	"taskAdd": "/views/task/taskAdd.html", //任务新增
	"taskEdit": "/views/task/taskEdit.html", //任务编辑
};

// 封装form数据为JSON
function formData(objStr) {
	var data = {};
	var strParam = $(objStr).serializeArray();
	$.each(strParam, function() {
		data[this.name] = this.value;
	});
	return data;
}

/** ajax请求 start **/
//发送POST请求ajax
function ajaxPost(url, param, func) {
	$.ajax({
		url: url,
		type: 'POST',
		dataType: 'json',
		contentType: 'application/json;charset=utf-8',
		data: JSON.stringify(param),
		success: function(resultData) {
			func(resultData);
		}, 
		error: function(xhr, status, e) {
			func({ "status": errorCode, "message": "请求数据失败" });
		}
	});
}

//上传文件ajax
function ajaxFile(url, methodType, formData, func) {
	$.ajax({
		url: url,
		type: methodType,
		processData: false,
		contentType: false,
		data: formData,
		dataType: 'json',
		success: function(resultData) {
			func(resultData);
		}, 
		error: function(xhr, status, e) {
			func({"status": errorCode, "message": "请求数据失败"});
		}
	})
}
/** ajax请求 end **/

// datagrid 分页参数过滤
function datagridFilter(resultData) {
	if (resultData.status == successCode) {
		var data = [];
		try {
			data = (
				null == resultData.data || null == resultData.data.records ? [] : { "rows": resultData.data.records, "total": resultData.data.total }
			);
		} catch(e) {}
		var opts = $(this).datagrid('options');
		var pager = $(this).datagrid('getPager');
		pager.pagination('refresh', { pageNumber: opts.pageNumber, pageSize: opts.pageSize });
		return data;
	} else {
		return { "status": resultData.status, "message": resultData.message, "rows": [], "total": 0 };
	}
}

// datagrid 点击行不选中
function datagridRowUnselect(rowIndex, rowData) {
	$(this).datagrid('unselectRow', rowIndex);
}

// datagrid 请求失败提示
function datagridReqErr(thisObj, resultData, field, colNum) {
	var json = {};
	json[field] = '<div style="text-align:center;color:red">' + resultData.message + '</div>';
	if (resultData.status && resultData.status != successCode) {
		$(thisObj).datagrid('appendRow', json).datagrid('mergeCells', { index: 0, field: field, colspan: colNum });
	}
}

// datagrid 初始化
function datagridInit(fid, fcolumns, ftitle, ftoolbar, floadData, ffun) {
	var datagridCfg = {
		loadMsg: '数据加载中...',
		fit: true,
		striped: true,
		singleSelect: true,
		border: false,
		pagination: true,
		columns: fcolumns,
		loader: floadData,
		loadFilter: datagridFilter,
		onClickRow: datagridRowUnselect,
		onLoadSuccess: ffun
	}
	if (ftitle != null && ftitle.trim() != '') datagridCfg.title = ftitle;
	if (ftoolbar != null && ftoolbar.trim() != '') datagridCfg.toolbar = '#' + ftoolbar;
	$(fid).datagrid(datagridCfg);
}
function datagridInitER(fid, fcolumns, floadData, ffun) {
	datagridInit(fid, fcolumns, null, null, floadData, ffun);
}